package it.ionize;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenu;
import javax.swing.JMenuItem;

import net.sourceforge.squirrel_sql.client.IApplication;
import net.sourceforge.squirrel_sql.client.gui.desktopcontainer.DialogWidget;
import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
import net.sourceforge.squirrel_sql.client.session.ISession;
import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;

public class OpenWindgetMenu extends JMenu{

		private static ILogger s_log = LoggerController.createLogger(OpenWindgetMenu.class);
        
        public OpenWindgetMenu(BlobExtractorPlugin fulltextsearchPlugin, ISession session) {  
            super("DanPlugin");  
        	s_log.debug("Messaggio da Danplugin");
        	
            JMenuItem jMenuItem = new JMenuItem("LobExporter");
            final IApplication app=fulltextsearchPlugin.getApplication();
            
            final ISession mySession= fulltextsearchPlugin.getApplication().getSessionManager().getActiveSession();
            
            final ISQLPanelAPI sqlPanel=session.getSQLPanelAPIOfActiveSessionWindow();
        
           
            
            jMenuItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {

					List<String> campi = new ArrayList<String>();
					List<String> campiBlob = new ArrayList<String>();
			            
		            ISQLConnection connection = mySession.getSQLConnection();  
		            PreparedStatement statement=null;
		            
		            try {
			            statement = connection.prepareStatement(sqlPanel.getSelectedSQLScript());
			            ResultSet resultSet = statement.executeQuery();  
			        	ResultSetMetaData metaData = resultSet.getMetaData();
			        	
						s_log.debug("DanColumns:"+metaData.getColumnCount());
			            for (int i = 1; i < metaData.getColumnCount()+1; i++) {
		            		s_log.debug("DanC:"+metaData.getColumnName(i)+" = "+metaData.getColumnType(i));
		            		campi.add(metaData.getColumnName(i));
			            	if(Types.BLOB==metaData.getColumnType(i) || Types.CLOB==metaData.getColumnType(i))
			            	{
			            		campiBlob.add(metaData.getColumnName(i));
			            		s_log.debug("Blob:"+metaData.getColumnName(i));
			            	}
		                }
			            
		            } catch (SQLException e) {
			        	s_log.error( e.getMessage(),e);
		            } 
		            finally
		            {
			            try {
			            	if(statement!=null)
			            	{
			            		statement.close();
			            		
			            	}
                        } catch (SQLException e) {
                        	//impossibile chiude lo statement?
                        }
		            }
		            


			            
					DialogWidget frame = new MyWidget("Lob Exporter options",true,app, sqlPanel.getSelectedSQLScript(),campi,campiBlob);

					app.getMainFrame().addWidget(frame);
					frame.pack();

//					frame.setSize(new Dimension(400, 250));

					DialogWidget.centerWithinDesktop(frame);
					
					frame.setVisible(true);
			        frame.moveToFront();
					
				}
			});
			add(jMenuItem);  
//            add(new SearchAction(fulltextsearchPlugin, session));  
        }  
      
}
